package com.ekang.admin.web;

import com.ekang.admin.common.BaseResult;
import com.ekang.admin.model.Member;
import com.ekang.admin.model.User;
import com.ekang.admin.service.UserService;
import com.ekang.easeim.api.impl.EasemobIMUsers;
import com.ekang.easeim.comm.TokenUtil;
import io.swagger.client.model.RegisterUsers;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by zcz on 2017/10/25.
 */
@Controller
public class UserController {

    private static Log log = LogFactory.getLog(UserController.class);

    @Autowired
    UserService userService;


    @RequestMapping(value = "/member", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity<BaseResult> updateMember(@RequestBody Member member) throws Exception {

        userService.updateMember(member);

        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(member);

        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }

    @RequestMapping(value = "/member", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<BaseResult> addMember(@RequestBody Member member) throws Exception {

        userService.addMember(member);

        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(member);

        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }

    @RequestMapping(value = "/member/wx", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<BaseResult> getWxMember(@RequestBody Member member) throws Exception {

        Object data = userService.getWxMember(member);

        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(data);

        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }

    @RequestMapping(value = "/member/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> getMember(@PathVariable("id") Integer id) throws Exception {

        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(userService.getMemberById(id));
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/friends/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> getMemberFriends(@PathVariable("id") Integer id) throws Exception {

        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(userService.getMemberListByUserId(id));
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping("/user")
    @ResponseBody
    public ResponseEntity<BaseResult> getUser(@RequestParam("userID") Integer userID) throws Exception {
        User user = userService.getUserById(userID);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(user);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping(value = "/adduser", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<BaseResult> addtUser() throws Exception {
        //User user = userService.getUserById(userID);


        log.info("im getAccessToken===" + TokenUtil.getAccessToken());

        EasemobIMUsers easemobIMUsers = new EasemobIMUsers();

        RegisterUsers users = new RegisterUsers();
        io.swagger.client.model.User user = new io.swagger.client.model.User().username("aaaa123456").password("123456");
        users.add(user);
        Object result = easemobIMUsers.createNewIMUserSingle(users);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(result);

        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


    @RequestMapping("/goods")
    @ResponseBody
    public ResponseEntity<BaseResult> getGoodsList() throws Exception {
        User user = userService.getUserById(1);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode("1");
        baseResult.setData(user);
        return new ResponseEntity(baseResult, null, HttpStatus.OK);
    }


}
